package component.fulltextsearch.index;

import java.io.IOException;
import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;

/**
 * 简易字符分割分析器
 * 作用:在索引分词的时候,碰到指定的字符亦拆分进行分词
 *
 */
public class CharStopAnalyzer extends Analyzer {

	public static final char[] ENGLISH_STOP_CHARS = { ';', '，','；' ,'，'};

	private final char[] stopChars;

	public CharStopAnalyzer() {
		this.stopChars = ENGLISH_STOP_CHARS;
	}

	public CharStopAnalyzer(char[] stopChars) {
		this.stopChars = stopChars;
	}

	public TokenStream tokenStream(String fieldName, Reader reader) {
		return new StopCharTokenizer(reader, stopChars);
	}

	public TokenStream reusableTokenStream(String fieldName, Reader reader)
			throws IOException {
		Tokenizer tokenizer = (Tokenizer) getPreviousTokenStream();
		if (tokenizer == null) {
			tokenizer = new StopCharTokenizer(reader, stopChars);
			setPreviousTokenStream(tokenizer);
		} else {
			tokenizer.reset(reader);
		}
		return tokenizer;
	}
}